package com.smart.seven.aop.pointcutadvisor.dynamicmethod;

import com.smart.seven.aop.pointcutadvisor.Waiter;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 动态切面
 *
 * @author junrui.wang
 * @create 2019/2/12
 */
public class GreetingDynamicPointcut extends DynamicMethodMatcherPointcut {

    private static List checkNameList = new ArrayList<String>();
    static {
        checkNameList.add("Tom");
        checkNameList.add("Jack");
    }

    @Override
    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            @Override
            public boolean matches(Class<?> clazz) {
                System.out.println("调用getClassFilter()对" + clazz + "进行静态检查.");
                return Waiter.class.isAssignableFrom(clazz);
            }
        };
    }

    @Override
    ///对方法进行静态切点检查
    public boolean matches(Method method, Class<?> clazz) {
        System.out.println("调用matches(method,clazz)对" + clazz.getName() + "." + method.getName() + "进行静态检查.");
        return "greetTo".equalsIgnoreCase(method.getName());
    }

    //对方法进行动态切点检查
    public boolean matches(Method method, Class<?> clazz, Object[] args) {
        System.out.println("调用matches(method,clazz)对" + clazz.getName() + "." + method.getName() + "进行动态检查.");

        String name = (String) args[0];

        return checkNameList.contains(name);
    }
}
